#include "widget.h"
#include "ui_widget.h"

extern uint8_t Tx_data[6];

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    ctrl_prog = new Ctrl_prog(this);
    mythread = new Mythread(this);
    connect(mythread, SIGNAL(adc_task_signal(QString)), this, SLOT(adc_data_slot(QString)));
    mythread->start();
}

Widget::~Widget()
{
    delete ui;
}



void Widget::on_led_r_clicked(bool checked)
{
    //设置设备号 0x1->LED
    Tx_data[1] = 0x1;
    if( checked == 1 ){
        Tx_data[2] = 0x1;
        Tx_data[3] = 0x1;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    else{
        Tx_data[2] = 0x1;
        Tx_data[3] = 0x0;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    Tx_data[1] = 0;
    Tx_data[2] = 0;
    Tx_data[3] = 0;
    Tx_data[4] = 0;
}

void Widget::on_led_g_clicked(bool checked)
{
    //设置设备号 0x1->LED
    Tx_data[1] = 0x1;
    if( checked == 1 ){
        Tx_data[2] = 0x2;
        Tx_data[3] = 0x1;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    else{
        Tx_data[2] = 0x2;
        Tx_data[3] = 0x0;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    Tx_data[1] = 0;
    Tx_data[2] = 0;
    Tx_data[3] = 0;
    Tx_data[4] = 0;
}

void Widget::on_led_b_clicked(bool checked)
{
    //设置设备号 0x1->LED
    Tx_data[1] = 0x1;
    if( checked == 1 ){
        Tx_data[2] = 0x3;
        Tx_data[3] = 0x1;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    else{
        Tx_data[2] = 0x3;
        Tx_data[3] = 0x0;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    Tx_data[1] = 0;
    Tx_data[2] = 0;
    Tx_data[3] = 0;
    Tx_data[4] = 0;
}

void Widget::on_beep_clicked(bool checked)
{
    //设置设备号 0x2->BEEP
    Tx_data[1] = 0x2;
    if( checked == 1 ){
        Tx_data[2] = 0x1;
        Tx_data[3] = 0x0;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    else{
        Tx_data[2] = 0x0;
        Tx_data[3] = 0x0;
        Tx_data[4] = (uint8_t)(Tx_data[1]+Tx_data[2]+Tx_data[3]);
        ctrl_prog->qt_send(Tx_data);
    }
    Tx_data[1] = 0;
    Tx_data[2] = 0;
    Tx_data[3] = 0;
    Tx_data[4] = 0;
}

void Widget::adc_data_slot(QString str)
{
    ui->adc_line->setText(str);
    printf("1\n");
}


